package com.gibs.peixunapi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.gibs.peixunapi.utils.ConverterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.validation.Valid;

import org.springframework.transaction.annotation.Transactional;
import com.gibs.peixunapi.result.PageData;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.utils.ResultUtil;
import com.gibs.peixunapi.dao.ActionLogDao;
import com.gibs.peixunapi.service.ActionLogService;
import com.gibs.peixunapi.model.ActionLog;

@Service
@Slf4j
public class ActionLogServiceImpl implements ActionLogService {

    @Autowired
    private ActionLogDao actionLogDao;

    @Transactional
    @Override
    public Result create(@Valid ActionLog object) {
        try {

            actionLogDao.save(object);
            return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Transactional
    @Override
    public Result save(@Valid ActionLog object) {
        try {

            ActionLog actionLog = actionLogDao.getOne(object.getId());
            ConverterUtil.copyNotNullProperties(object, actionLog);
            actionLogDao.save(actionLog);
            return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result delete(Integer id) {
        try {
//            String[] ids = id.split(",");
//            for (String str : ids) {
//                actionLogDao.delete(Integer.valueOf(str));
//            }
            return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result deleteBatch(String id) {
        try {
            String[] ids = id.split(",");
            for (String str : ids) {
//                actionLogDao.delete(Integer.valueOf(str));
            }
            return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }


    @Override
    public Result get(Integer id) {
        try {
            HashMap<String, Object> data = new HashMap<String, Object>();
            ActionLog object = actionLogDao.getOne(id);
            if (object == null) {
                ResultUtil.fail(ResultEnum.FIND_FAIL);
            }
            HashMap<String, Object> map = new HashMap();
            map.put("id", object.getId());
            map.put("actionPath", object.getActionPath() == null ? "" : object.getActionPath());
            HashMap<String, Object> userId = new HashMap<String, Object>();
            userId.put("name", object.getUserId() == null ? "" : object.getUserId().getName());
            userId.put("id", object.getUserId() == null ? "" : object.getUserId().getId());
            map.put("userId", userId);
//            map.put("actionTime", object.getActionTime() == null ? "" : DateUtils.toString(object.getActionTime()));
            map.put("content", object.getContent() == null ? "" : object.getContent());
            map.put("beforeData", object.getBeforeData() == null ? "" : object.getBeforeData());
            map.put("afterData", object.getAfterData() == null ? "" : object.getAfterData());
            map.put("method", object.getMethod() == null ? "" : object.getMethod());
            map.put("token", object.getToken() == null ? "" : object.getToken());
            map.put("requestParam", object.getRequestParam() == null ? "" : object.getRequestParam());
            data.put("object", map);

            return ResultUtil.success(ResultEnum.FIND_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result getList(String key) {
        try {
            List<ActionLog> list = new ArrayList<>();

            if (!StringUtils.isEmpty(key)) {
                list = actionLogDao.findLike("%" + key + "%");
            } else {
                list = actionLogDao.findAll();
            }

            if (list.size() == 0) {
                PageData pageData = new PageData(0, 0, 0);
                ResultUtil.successPage(ResultEnum.DATA_NOT_FIND);
            }
            List<Object> data = new ArrayList<>();
            for (ActionLog object : list) {
                HashMap<String, Object> map = new HashMap();
                map.put("id", object.getId());
                map.put("actionPath", object.getActionPath() == null ? "" : object.getActionPath());
                HashMap<String, Object> userId = new HashMap<String, Object>();
                userId.put("name", object.getUserId() == null ? "" : object.getUserId().getName());
                userId.put("id", object.getUserId() == null ? "" : object.getUserId().getId());
                map.put("userId", userId);
//                map.put("actionTime", object.getActionTime() == null ? "" : DateUtils.toString(object.getActionTime()));
                map.put("content", object.getContent() == null ? "" : object.getContent());
                map.put("beforeData", object.getBeforeData() == null ? "" : object.getBeforeData());
                map.put("afterData", object.getAfterData() == null ? "" : object.getAfterData());
                map.put("method", object.getMethod() == null ? "" : object.getMethod());
                map.put("token", object.getToken() == null ? "" : object.getToken());
                map.put("requestParam", object.getRequestParam() == null ? "" : object.getRequestParam());
                data.add(map);
            }
            return ResultUtil.success(ResultEnum.FIND_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result getListPage(String key, Integer current, Integer limit) {
        try {
            List<ActionLog> list = new ArrayList<>();
            limit = limit == null ? 10 : limit;
            current = current == null ? 1 : current;
            int offset = (current - 1) * limit;
            Long total = null;
            if (!StringUtils.isEmpty(key)) {
                list = actionLogDao.findPageLike("%" + key + "%", offset, limit);
                total = actionLogDao.countLike("%" + key + "%");
            } else {
                list = actionLogDao.findPage(offset, limit);
                total = actionLogDao.count();
            }
            if (total == 0) {
//                PageResult(true, Status.OK, "无数据", new ArrayList(), 0L, 0, 0);
            }
            List<Object> data = new ArrayList<>();
            for (ActionLog object : list) {
                HashMap<String, Object> map = new HashMap();
                map.put("id", object.getId());
                map.put("actionPath", object.getActionPath() == null ? "" : object.getActionPath());
                HashMap<String, Object> userId = new HashMap<String, Object>();
                userId.put("name", object.getUserId() == null ? "" : object.getUserId().getName());
                userId.put("id", object.getUserId() == null ? "" : object.getUserId().getId());
                map.put("userId", userId);
//                map.put("actionTime", object.getActionTime() == null ? "" : DateUtils.toString(object.getActionTime()));
                map.put("content", object.getContent() == null ? "" : object.getContent());
                map.put("beforeData", object.getBeforeData() == null ? "" : object.getBeforeData());
                map.put("afterData", object.getAfterData() == null ? "" : object.getAfterData());
                map.put("method", object.getMethod() == null ? "" : object.getMethod());
                map.put("token", object.getToken() == null ? "" : object.getToken());
                map.put("requestParam", object.getRequestParam() == null ? "" : object.getRequestParam());
                data.add(map);
            }
            PageData pageData = new PageData(0, limit, current);
            return ResultUtil.successPage(ResultEnum.SUCCESS, pageData);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }


    @Override
    public Result getOptions(String key) {
        try {
            List<ActionLog> list = new ArrayList<>();
            if (!StringUtils.isEmpty(key)) {
                list = actionLogDao.findLike("%" + key + "%");
            } else {
                list = actionLogDao.findAll();
            }
            List<Object> data = new ArrayList<>();
            for (ActionLog object : list) {
                HashMap<String, Object> map = new HashMap();
                map.put("value", object.getId());
                map.put("name", object.getActionPath());
                data.add(map);
            }
            return ResultUtil.success(ResultEnum.SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }


}

